package com.briup.corejava.day15;

/**
 * 线程创建：实现Runnable接口
 */
public class Test4 {
    public static void main(String[] args) {
        Runnable r = new MyRunnable();
        Thread t1 = new Thread(r,"t1线程");

        // 使用匿名内部类创建线程t2
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
                for(int i=11;i<20;i++){
                    System.out.println("thread hello");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        },"t2线程");

        t1.start();
        t2.start();
    }
}
class MyRunnable implements Runnable{

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        for(int i=0;i<10;i++) {
            System.out.println(name + ", hello");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
